package com.sky.tool.maven.plugins

import org.apache.maven.plugin.AbstractMojo
import org.apache.maven.plugin.logging.Log
import org.apache.maven.plugins.annotations.Component
import org.apache.maven.plugins.annotations.Parameter
import org.apache.maven.project.MavenProject
import org.codehaus.plexus.util.StringUtils

public abstract class AbstractServiceMojo extends AbstractMojo {
    @Component
    protected MavenProject project;
    @Parameter(defaultValue = "\${project.build.directory}/dist")
    protected File targetDir;
    @Parameter(defaultValue = "\${project.description}")
    protected String description;

    @Parameter
    protected String vmOptions;

    @Parameter
    protected String programArguments;
    @Parameter(required = true, defaultValue = "\${project.artifactId}")
    protected String appId;
    @Parameter(required = true, defaultValue = "\${build.finalName}")
    protected String appName;
    @Parameter(required = true, defaultValue = "\${build.finalName}.\${project.packaging}")
    protected String runJarName;


    protected abstract void generate();

    private void paraterValid() {
        if (null == targetDir) {
            targetDir = new File(project.getBuild().getDirectory());
        }
        if (StringUtils.isEmpty(appId)) {
            appId = project.getArtifactId();
        }
        if (StringUtils.isEmpty(appName)) {
            appName = project.getBuild().getFinalName();
        }
        if (StringUtils.isEmpty(description)) {
            description = project.getDescription();

        }
        if (StringUtils.isEmpty(description)) {
            description = project.getName();
        }
        if (StringUtils.isEmpty(description)) {
            description = project.getArtifactId();
        }
        if (StringUtils.isEmpty(runJarName)) {
            runJarName = project.getArtifactId()+"."+project.getPackaging();
        }
        Log log = getLog();
        log.info("project:" + project);
        log.info("targetDir:" + targetDir);
        log.info("description:" + description);
        log.info("vmOptions:" + vmOptions);
        log.info("programArguments:" + programArguments);
        log.info("appId:" + appId);
        log.info("appName:" + appName);
        log.info("runJarName:" + runJarName);
    }

    @Override
    public void execute() {
        paraterValid();
        generate();
    }
}
